package com.boot.dog.common.util;

import java.io.File;
import java.util.Arrays;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.boot.dog.common.exception.DogException;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: MaxLv
 * @Date: 2020/1/18
 */
@Slf4j
@UtilityClass
public class FileStoreUtil {

    private static final long IMG_MAX_SIZE = 1024 * 1024 * 5;

    private static final String[] IMG_TYPE = new String[]{"jpg", "jpeg", "png", "gif", "bmp"};

    public static String saveImgToTemp(DogMultipartFile file) {
        return saveValidFile(file, IMG_TYPE, IMG_MAX_SIZE, FileUtil.getTempFolder());
    }

    /**
     * 保存文件到Temp
     *
     * @param file
     * @param maxSize
     * @return
     */
    public String saveFileToTemp(DogMultipartFile file, long maxSize) {
        return saveValidFile(file, null, maxSize, FileUtil.getTempFolder());
    }

    public String saveFileToTemp(DogMultipartFile file, long maxSize, String[] validType) {
        return saveValidFile(file, validType, maxSize, FileUtil.getTempFolder());
    }

    public String saveImgToNas(DogMultipartFile file, String folder) {
        return saveValidFile(file, IMG_TYPE, IMG_MAX_SIZE, FilenameUtils.concat(FileUtil.getNasFolder(), folder));
    }

    /**
     * 保存文件到Nas
     *
     * @param file
     * @param maxSize
     * @return
     */
    public String saveFileToNas(DogMultipartFile file, long maxSize) {
        return saveValidFile(file, null, maxSize, FileUtil.getNasFolder());
    }

    /**
     * 保存并验证文件
     *
     * @param file      文件内容
     * @param validType 验证类型
     * @param maxSize   最大大小
     * @param folder    保存位置
     * @return
     */
    public String saveValidFile(DogMultipartFile file, String[] validType, long maxSize, String folder) {

        // 验证文件类型
        if (!validateFileType(file.getFilename(), validType)) {
            throw new DogException("文件类型错误,只能为jpg、jpeg、png、gif、bmp");
        }

        // 验证文件大小
        if (isEmpty(file)) {
            throw new DogException("文件大小不能为0");
        }
        if (!validateFileSize(file, maxSize)) {
            throw new DogException("文件大小不能超过5M");
        }
        try {
            // 保存文件
            return store(file, folder);
        } catch (Exception e) {
            log.error("Error: fileStoreUtil saveValidFile={}", e);
            throw new DogException("文件保存异常");
        }
    }

    /**
     * 验证文件类型
     *
     * @param filename 文件名
     * @param types    类型
     * @return
     */
    public boolean validateFileType(String filename, String... types) {
        if (CollUtil.isEmpty(Arrays.stream(types).collect(Collectors.toList()))) {
            return true;
        }
        String ext = FilenameUtils.getExtension(filename).toLowerCase();
        for (String type : types) {
            if (type.equals(ext)) {
                return true;
            }
        }
        return false;
    }

    public boolean isEmpty(DogMultipartFile file) {
        if (file.getSize() == 0) {
            return true;
        }
        return false;
    }

    public boolean isNotEmpty(DogMultipartFile file) {
        return !isEmpty(file);
    }

    public boolean validateFileSize(DogMultipartFile file, long maxSize) {
        if (maxSize > 0 && file.getSize() > maxSize) {
            return false;
        }
        return true;
    }

    @SneakyThrows
    private String store(DogMultipartFile file, String dir) {
        String ext = FilenameUtils.getExtension(file.getFilename());
        String newFilename = CommonUtil.getUUID() + FilenameUtils.EXTENSION_SEPARATOR_STR + ext;
        File targetFile = new File(dir, newFilename);
        FileUtils.copyInputStreamToFile(file.getInputstream(), targetFile);
        return newFilename;
    }

}